﻿(function () {
    appModule.controller('common.views.gridPersons.createOrEditModal', [
        '$scope', '$uibModalInstance', 'abp.services.app.gridPerson', 'gridPersonId', 'gridPerson', 'abp.services.app.area', 'abp.services.app.personType', 'textAngularManager',
        function ($scope, $uibModalInstance, gridPersonService, gridPersonId, gridPerson, areaService, personTypeService, textAngularManager) {
            var vm = this;
            vm.saving = false;
            //首先将gridPerson数据设置为null
            vm.gridPerson = null;

            //触发保存方法
            vm.save = function () {
                vm.saving = true;
                gridPersonService.createOrUpdateGridPersonAsync({ gridPersonEditDto: vm.gridPerson }).then(function () {
                    abp.notify.info(app.localize('SavedSuccessfully'));
                    $uibModalInstance.close();
                }).finally(function () {
                    vm.saving = false;
                });
            };
            //取消关闭页面
            vm.cancel = function () {
                $uibModalInstance.dismiss();
            };

            //初始化页面
            function init() {
                //绑定镇控件
                areaService.getAreaByLevelAsync({ id: 0 }).then(function (result) {
                    vm.townList = result.data.items;
                });
                //绑定性别
                var sexData = { data: [{ 'name': '男', 'value': true }, { 'name': '女', 'value': false }] };
                vm.sexList = sexData.data;

                //绑定人员类别控件
                personTypeService.getAllPersonTypeListAsync().then(function (result) {
                    vm.personTypeList = result.data.items;
                });

                if (gridPersonId != null) {
                    //绑定区控件
                    areaService.getAreaByLevelAsync({ id: gridPerson.townID }).then(function (result) {
                        vm.areaList = result.data.items;
                    });

                    //绑定村控件
                    areaService.getAreaByLevelAsync({ id: gridPerson.areaID }).then(function (result) {
                        vm.villageList = result.data.items;

                        //绑定数据
                        gridPersonService.getGridPersonForEditAsync({
                            id: gridPersonId
                        }).then(function (result) {
                            vm.gridPerson = result.data.gridPerson;
                        });
                    });
                }
                else {
                    gridPersonService.getGridPersonForEditAsync({
                        id: gridPersonId
                    }).then(function (result) {
                        vm.gridPerson = result.data.gridPerson;
                    });
                }
            }

            //绑定区
            vm.bindArea = function (townID) {
                areaService.getAreaByLevelAsync({ id: townID }).then(function (result) {
                    vm.areaList = result.data.items;
                });
            };

            //绑定村
            vm.bindVillage = function (areaID) {
                areaService.getAreaByLevelAsync({ id: areaID }).then(function (result) {
                    vm.villageList = result.data.items;
                });
            };

            //执行初始化方法
            init();
        }
    ]);
})();